' projects.bmx

Strict

Import "host.bmx"


Type TProjectRequester Extends TRequester
	Field	projects:TProjects
	Field	listbox:TGadget
	Field	add:TGadget
	Field	remove:TGadget
	Field	props:TGadget
	Field	moveup:TGadget
	Field	movedown:TGadget
	Field	current:TProjectFolderNode

	Method Invoke(command$,arg:Object=Null)
		Select command
		Case "ACTIVATE" 
			Refresh
		End Select
	End Method

	Method SetCurrent(i)
		If i=-1 
			DisableGadget remove
			DisableGadget moveup
			DisableGadget movedown
			DisableGadget props 
			current=Null
		Else
			current=TProjectFolderNode(GadgetItemExtra(listbox,i))
			If current 
				EnableGadget remove
				EnableGadget props 
				EnableGadget moveup
				EnableGadget movedown
			EndIf
		EndIf
	End Method

	Method OnEvent()
		Local	id,data,index
		id=EventID()
		data=EventData()
		Select EventSource()
			Case window
				If EventID()=EVENT_WINDOWCLOSE
					Hide
				EndIf
			Case add
				projects.NewProject
				Refresh
			Case remove
				projects.RemoveProject SelectedGadgetItem(listbox)
				Refresh
			Case ok
				Hide				
			Case cancel
				Hide
			Case props
				If current current.OpenProps
			Case listbox
				If EventID()=EVENT_GADGETSELECT
					SetCurrent SelectedGadgetItem(listbox)	'EventData()
				EndIf
			Case moveup
				index=projects.MoveProject(SelectedGadgetItem(listbox),-1)
				Refresh
				SelectGadgetItem listbox,index
			Case movedown
				index=projects.MoveProject(SelectedGadgetItem(listbox),1)
				Refresh
				SelectGadgetItem listbox,index
		End Select
	End Method

	Method Refresh()
		Local	node:TDataNode		
		ClearGadgetItems listbox
		For node=EachIn projects.kids
			If TFolderNode(node)'node.argument
				AddGadgetItem listbox,node.name,0,-1,"",node
			EndIf
		Next
		SetCurrent -1
	End Method
	
	Method Open(projnode:TProjects)
		projects=projnode
		Refresh
		Show
	End Method
	
	Function Create:TProjectRequester(host:THost)
		Local x,y
		Local	proj:TProjectRequester
		proj=New TProjectRequester
	
		proj.initrequester(host,"Project Manager",400,256)
		proj.listbox=CreateListBox( 8,8,240,180,proj.window )
		
		x=ClientWidth(proj.window)-136
		proj.add=CreateButton("Add Project",x,8,128,24,proj.window)
		proj.remove=CreateButton("Remove Project",x,40,128,24,proj.window)

		proj.moveup=CreateButton("Move Up",x,72,128,24,proj.window)
		proj.movedown=CreateButton("Move Down",x,104,128,24,proj.window)

		proj.props=CreateButton("Properties",x,136,128,24,proj.window)

		DisableGadget proj.remove
		DisableGadget proj.moveup
		DisableGadget proj.movedown
		DisableGadget proj.props 
		Return proj
	End Function
End Type

Type TProjectProperties Extends TRequester
	Field	proj:TProjectFolderNode
	Field	localname:TGadget
	Field	localpath:TGadget
	Field	pathbutton:TGadget
	Field	path:TGadget
	Field	user:TGadget
	Field	password:TGadget
	Field	checkout:TGadget
'	Field	update:TGadget
'	Field	commit:TGadget
	Field	poprequester:TRequester	'hack for restoring to projectmanager requester
	Field	dirty

	Method Invoke(command$,arg:Object=Null)
		Select command
		Case "ACTIVATE" 
			Refresh
		End Select
	End Method
	
	Method Tidy()
		If dirty
			proj.Set GadgetText(localname),GadgetText(localpath),GadgetText(path),GadgetText(user),GadgetText(password)
			dirty=False
		EndIf
	End Method
	
	Method Poll()
		Select EventSource()
			Case localname,localpath,path,user,password
				dirty=True
			Case pathbutton
				Local dir$=RequestDir("Select the project folder")
				If dir
					If dir[dir.length-1..]="/"	'fltk hack
						dir=dir[..dir.length-1]
					EndIf
					SetGadgetText localpath,dir
					If GadgetText(localname)=""
						SetGadgetText localname,StripDir(dir)
					EndIf
					dirty=True
				EndIf				
			Case checkout
				Tidy
				Hide				
				proj.CheckoutVersion		
Rem
			Case commit
				Tidy
				Hide				
				proj.CommitVersion		
			Case update
				Tidy
				Hide				
				proj.UpdateVersion		
EndRem
			Case ok
				Tidy
				Hide				
			Case cancel
				Hide
			Case window
				If EventID()=EVENT_WINDOWCLOSE
					Hide
				EndIf
		End Select
	End Method
	
	Method Hide()
		EnableGadget host.window
		HideGadget window
		host.SetRequester Null'poprequester
		If poprequester poprequester.Show
	End Method

	Method Refresh()
		SetGadgetText localname,proj.name
		SetGadgetText localpath,proj.path
		SetGadgetText path,proj.svnpath
		SetGadgetText user,proj.svnuser
		SetGadgetText password,proj.svnpass
	End Method
	
	Method Open(projnode:TProjectFolderNode)
		proj=projnode
		poprequester=host.activerequester
		Refresh
		Show
	End Method
	
	Function Create:TProjectProperties(host:THost)
		Local	proj:TProjectProperties

		proj=New TProjectProperties
		proj.initrequester(host,"Project Properties",512,380)
		Local i,n,y
		y=18

		CreateLabel("Name",8,y+4,72,24,proj.window)
		proj.localname=CreateTextField(88,y,340,20,proj.window)
'		proj.pathbutton=CreateButton("..",434,y,34,28,proj.window)
		y:+30

		CreateLabel("Path",8,y+4,72,24,proj.window)
		proj.localpath=CreateTextField(88,y,340,20,proj.window)
		proj.pathbutton=CreateButton("..",434,y,34,28,proj.window)
		y:+30


		Local svnbox:TGadget
		svnbox=CreatePanel(4,y,500,240-y,proj.window,PANEL_GROUP)
		SetGadgetText svnbox,"Subversion Control"
		y=12
		CreateLabel("URL",8,y+LABELOFF,72,24,svnbox)
		proj.path=CreateTextField(88,y,380,20,svnbox)
		y:+30
		CreateLabel("Username",8,y+LABELOFF,72,24,svnbox)
		proj.user=CreateTextField(88,y,380,20,svnbox)
		y:+30
		CreateLabel("Password",8,y+LABELOFF,72,24,svnbox)
		proj.password=CreateTextField(88,y,380,20,svnbox,TEXTFIELD_PASSWORD)		
		y:+30
		proj.checkout=CreateButton("Check Out Project",340,y+10,150,28,svnbox)
'		proj.update=CreateButton("Update Project",180,y+10,150,28,svnbox)
'		proj.commit=CreateButton("Commit Project",340,y+10,150,28,svnbox)
		y:+40

		Return proj
	End Function
End Type

Function GetInfo$(a$ Var)
	Local 	p,r$
	p=a.Find("|")+1
	If p=0 p=Len a$+1
	r$=a$[..p-1]
	a$=a$[p..]
	Return r$	
End Function

Type TFolderNode Extends TDataNode
'	Field	host:THost
	Field	owner:TDataNode
	Field	path$
	Field	filters$
	Field	scanned
	Field	version
	Field	foldertype
	
	Const PROJECTFOLDER=0
	Const DIRECTORYFOLDER=1
	Const FILEFOLDER=2

	Method FindFolderFromPath:TFolderNode(dir$)
		Local folder:TFolderNode
		Local result:TFolderNode
		If path=dir Return Self
		For folder=EachIn kids
			result=folder.FindFolderFromPath(dir)
			If result Return result
		Next
	End Method
	
	Method SetName(n$)
		If version n=n+"("+version+")"
		Super.SetName n
		Refresh
	End Method
		
	Method SetVersion(ver)
		Local n$	
		version=ver
		n=StripDir(path)
'		If version n=n+"("+version+")"
		SetName n
	End Method
	
	Method Write(stream:TStream,indent$)
		Local folder:TFolderNode
		Local isopen
		If GetState()=OPENSTATE isopen=True
		If version Or isopen
			stream.WriteLine indent+"proj_data="+path+"|"+isopen+"|"+version+"|"
		EndIf
		For folder=EachIn kids
			folder.Write(stream,indent)
		Next
	End Method

	Method ProjectRoot:TProjects()
		Local n:TDataNode
		n=Self
		While n
			If TProjects(n) Return TProjects(n)
			n=n.parent
		Wend
	End Method

	Method ProjectHost:THost()
		Local n:TDataNode
		n=Self
		While n
			If TProjects(n) Return TProjects(n).host
			n=n.parent
		Wend
	End Method

	Method ProjectNode:TProjectFolderNode()
		Local n:TDataNode
		n=Self
		While n
			If TProjectFolderNode(n) Return TProjectFolderNode(n)
			n=n.parent
		Wend
	End Method

	Method RunSVN(cmd$,about$,refresh)
		Local host:THost
		Local project:TProjectFolderNode
	
		host=ProjectHost()
		If Not host Notify"node host not found";Return
		
		project=ProjectNode()
		If Not project Notify"node project not found";Return
		
		If project.svnuser 
			cmd:+" --username "+project.svnuser
			If project.svnpass cmd:+" --password "+project.svnpass
		EndIf

		If refresh
			host.execute Self,cmd,about,MENUREFRESH,True
		Else
			host.execute Self,cmd,about,0,0
		EndIf
	End Method
	
	Method UpdateVersion()
		Local cmd$
		cmd$=svncmd+" update"
		cmd:+" "+quote(path)
		RunSVN cmd,"Updating "+path,True
'		host.execute cmd,"Updating "+path,MENUREFRESH,True,Self
	End Method
	
	Method CommitVersion()
		Local cmd$
		cmd=svncmd+" commit"
		cmd:+" -m ~qmy comment~q"
		cmd:+" "+quote(path)
		RunSVN cmd,"Committing "+path,False
'		host.execute cmd,"Committing "+path
	End Method
	
	Method Open(view=-1)
		If Not scanned
			ScanKids
		EndIf
		Super.Open view
	End Method

	Method AddFileNode:TDataNode(file$)
		Local	n:TDataNode
		Local 	ext$,filetypes$
			
'		If NodeFileTypes.find("|"+ExtractExt(file).toLower()+"|")<0 Return
		If filters.find("|"+ExtractExt(file).toLower()+"|")<0 Return
		n=AddNode(StripDir(file))
		n.SetAction(owner,"OPEN",file)
		ext=ExtractExt(file$).ToLower()
		n.sortname=ext+n.name
		Return n
	End Method

	Method AddFolderNode:TDataNode(path$,filters$)
		Local	n:TDataNode
		n=TFolderNode.CreateFolderNode(path,DIRECTORYFOLDER,filters$)
		n.sortname=" "+n.name
		Append n
		Return n
	End Method

	Method Scan(o:TDataNode)
		Local	flist:TList
		Local	dir,f$,p$,t	
		owner=o
		flist=New TList
		dir=ReadDir(path)
		If dir
			Repeat
				f$=NextFile(dir)
				If f$="" Exit
				If f[..1]="." Continue
				p$=path+"/"+f
				Select FileType(p$)
				Case FILETYPE_FILE
					AddFileNode p$
				Case FILETYPE_DIR
					AddFolderNode p$,filters
				End Select				
			Forever
			CloseDir dir
		EndIf
		SortKids
	End Method
	
	Method ScanKids()
		Local	f:TFolderNode		
		If scanned Return
		For f=EachIn kids
			f.Scan owner
		Next
		scanned=True
	End Method

	Method Rescan()
		FreeKids
		Scan owner
		scanned=False
		ScanKids
		Refresh
	End Method
	
	Method Invoke(command$,argument:Object=Null)
		Local host:THost
		Local projects:TProjects
		Local cmd,p
		Local line$
	
		host=ProjectHost()
		If Not host Notify"node host not found";Return
		
		Select command
		Case "OUTPUT"
			line$=String(argument)
			p=line.find(" revision ")
			If p>-1
				SetVersion Int(line[p+10..])
			EndIf
'			If line[..12]="At revision "
'			DebugLog "TOOLOUTPUT:"+line
			Return
		Case "ERROR"
			line$=String(argument)
'			DebugLog "TOOLERROR:"+line
			Return
		Case "MENU"
			cmd=Int(String(argument))
			Select cmd
			Case 0		'special toolmenu-command=0 fired by rightbutton node context
				Highlight			
				Local menu:TGadget
				projects=ProjectRoot()
				menu=projects.projmenu
				PopupWindowMenu host.window,menu,Self
			Case MENUREFRESH				
				Rescan
			Case MENUBROWSE				
				OpenURL RealPath(path)
			Case MENUSHELL				
'				system_ RealPath(path)
				Local cd$=CurrentDir()
				ChangeDir RealPath(path)
?MacOS
				Local s$="/bin/bash"' --help"
				host.execute s$,"Shell Terminal"
?Linux
				Local s$="/bin/bash"' --help"
				host.execute s$,"Shell Terminal"
?Win32
				host.execute Null,"cmd","Shell Terminal - Type Exit To End"
?
				ChangeDir cd
			Case MENUUPDATE
				UpdateVersion 
			Case MENUCOMMIT
				CommitVersion
'			Case MENUPROPS
'				host.projectprops.Open(Self)


			End Select
		End Select
	End Method

	Function CreateFolderNode:TFolderNode(path$,foldertype,filters$)
		Local	n:TFolderNode
'		print "create foldernode "+path
		n=New TFolderNode
'		n.host=host
		n.setname StripDir(path)
		n.path=path
		n.filters=filters
		n.foldertype=foldertype
		Return n
	End Function
End Type

Type TProjectFolderNode Extends TFolderNode
	Field 	owner:TProjects
	Field	svnpath$,svnuser$,svnpass$,svnversion
	Field	svnerr$

	Method CheckoutVersion()	'to do - needs to move old version to temp?
		Local cmd$
		cmd$=svncmd+" checkout"
		cmd:+" "+quote(svnpath)
		cmd:+" "+quote(path)
		RunSVN cmd,"Checking out "+svnpath+" to "+path,True
		'host.execute cmd,"Checking out "+svnpath+" to "+path,MENUREFRESH,True,Self
	End Method

	Function Crypt$(a$)
		Local b$,c,i
		For i=0 Until Len a
			c=a[i]
			If c>31 c:~((i*-5)&31)
			b:+Chr(c&255)
		Next
		Return b
	End Function

	Method ToString$()
		Local prj$
		Local isopen
		If GetState()&OPENSTATE isopen=True
		prj=name+"|"+path+"|"+svnpath+"|"+svnuser+"|"+crypt(svnpass)+"|"+isopen+"|"+version
		Return prj
	End Method

	Method Write(stream:TStream,indent$)
		Local folder:TFolderNode
		stream.WriteLine indent+"proj_node="+ToString()
		For folder=EachIn kids
			folder.Write(stream,indent)
		Next
	End Method
	
	Method FromString(info$)
		Local n$
		n$=GetInfo(info)
		If n="" n="Unknown"
		setname n
		path=GetInfo(info)
		If path path=owner.host.FullPath(path)
		svnpath=GetInfo(info)
		svnuser=GetInfo(info)
		svnpass=GetInfo(info)
		Scan(owner)
		Local isopen,vers
		isopen=Int(GetInfo(info))
		If isopen
			Open
		EndIf	
		vers=Int(GetInfo(info))
		If vers
			SetVersion vers
		EndIf
'		Notify "svnpass="+svnpass
	End Method

	Method OpenProps()
		Local projects:TProjects
		projects=ProjectRoot()
		projects.projectprops.Open(Self)'current)
	End Method

	Method Invoke(command$,argument:Object=Null)
		Local cmd
		Select command
		Case "MENU"
			cmd=Int(String(argument))
			Select cmd
			Case MENUPROPS
				Return OpenProps()		'owner.host.projectprops.Open(Self)
			End Select
		End Select
		Return Super.Invoke(command,argument)
	End Method

	Method Set(n$,p$,s$,user$,pass$)
		path=owner.host.FullPath(p)
		setname n
		svnpath=s
		svnuser=user
		svnpass=pass
		Rescan
	End Method

	Function CreateProjectNode:TProjectFolderNode(projects:TProjects,info$)
		Local n:TProjectFolderNode
		n=New TProjectFolderNode
		n.owner=projects
		n.FromString(info)
		n.foldertype=PROJECTFOLDER
		Return n
	End Function
End Type

Type TProjects Extends TDataNode
	Field host:THost
	Field addproj:TDataNode
	Field projmenu:TGadget
	Field projmenuprops:TGadget
	Field projectprops:TProjectProperties

	Method RemoveProject(index)
		Local	node:TDataNode
		If index<0 Or index>=kids.Count() Return
		node=TDataNode(kids.ValueAtIndex(index))
		If node node.Free
		Refresh
	End Method			
	
	Method MoveProject(index,dir)
		Local node:TDataNode
		Local link:TLink
		If index<0 Or index>=kids.Count() Return index
		node=TDataNode(kids.ValueAtIndex(index))
		If node 
			addproj.Detach
			node.Hide
			link=kids.FindLink(node)
			If dir>0
				If link link=link._succ
				If link 
					kids.Remove node
					kids.InsertAfterLink node,link
					index:+1
				EndIf
			Else
				If link link=link._pred
				If link 
					kids.Remove node
					kids.InsertBeforeLink node,link			
					index:-1
				EndIf
			EndIf		
			Append addproj
			Refresh
		EndIf
		Return index
	End Method			

	Method NewProject()
		Local proj:TProjectFolderNode
		addproj.Detach
		proj=TProjectFolderNode.CreateProjectNode(Self,"New Project")
'		proj.scan(Self)
		Append proj
		Append addproj
		Refresh
		projectprops.Open(proj)
	End Method

	Method AddProject(data:TList)
		Local info$
		Local project:TProjectFolderNode
		Local folder:TFolderNode
		For info=EachIn data
			If Not project
				addproj.Detach
				project=TProjectFolderNode.CreateProjectNode(Self,info)
				Append project
				Append addproj
				Refresh
			Else
				Local path$
				Local popen
				Local pversion
				path=GetInfo(info)
				popen=Int(GetInfo(info))
				pversion=Int(GetInfo(info))
				folder=project.FindFolderFromPath(path)
				If folder
					folder.SetVersion pversion
					If popen folder.Open
				EndIf			
			EndIf
		Next
	End Method

	Method Write(stream:TStream,indent$)
		Local	project:TProjectFolderNode
		For project=EachIn kids
			project.Write(stream,indent)		
		Next
	End Method

	Method Invoke(command$,argument:Object=Null)
		Select command
		Case "NEW"
			NewProject
		Case "OPEN"
			host.OpenSource String(argument)
		End Select
	End Method
			
	Function CreateProjects:TProjects(host:THost)
		Local	p:TProjects
		p=New TProjects
		p.SetName("Projects")
		p.host=host		
		p.addproj=p.AddNode("Add Project")
		p.addproj.SetAction p,"NEW"

		p.projmenu=CreateMenu("Proj",0,Null)
		CreateMenu "Refresh",MENUREFRESH,p.projmenu
		CreateMenu "Explore",MENUBROWSE,p.projmenu
		CreateMenu "Shell",MENUSHELL,p.projmenu
		CreateMenu "",0,p.projmenu
		CreateMenu "Update Version",MENUUPDATE,p.projmenu
		CreateMenu "Commit Version",MENUCOMMIT,p.projmenu
		CreateMenu "",0,p.projmenu
		p.projmenuprops=CreateMenu("Properties",MENUPROPS,p.projmenu)

		p.projectprops=TProjectProperties.Create(host)

		Return p
	End Function
End Type


Rem











Type TProjectRequester Extends TRequester
	Field	projects:TProjects
	Field	listbox:TGadget
	Field	add:TGadget
	Field	remove:TGadget
	Field	props:TGadget
	Field	moveup:TGadget
	Field	movedown:TGadget

	Method Invoke(command$,arg:Object=Null)
		Select command
		Case "ACTIVATE" 
			Refresh
		End Select
	End Method

	Method OnEvent()
		Local	id,data,index
		id=EventID()
		data=EventData()
		Select EventSource()
			Case window
				If EventID()=EVENT_WINDOWCLOSE
					Hide
				EndIf
			Case add
				projects.AddProject
				Refresh
			Case remove
				projects.RemoveProject SelectedGadgetItem(listbox)
				Refresh
			Case ok
				Hide				
			Case cancel
				Hide
			Case moveup
				index=projects.MoveProject(SelectedGadgetItem(listbox),-1)
				Refresh
				SelectGadgetItem listbox,index
			Case movedown
				index=projects.MoveProject(SelectedGadgetItem(listbox),1)
				Refresh
				SelectGadgetItem listbox,index
		End Select
	End Method

	Method Refresh()
		Local	node:TDataNode
		ClearGadgetItems listbox
		For node=EachIn projects.kids
			If TFolderNode(node)
				AddGadgetItem listbox,node.name
			EndIf
		Next
		projects.Refresh
	End Method
	
	Method Open(projnode:TProjects)
		projects=projnode
		Refresh
		Show
	End Method
	
	Function Create:TProjectRequester(host:THost)
		Local	proj:TProjectRequester
		proj=New TProjectRequester
		proj.initrequester(host,"Project Manager",400,256)
		proj.listbox=CreateListBox( 4,4,260,180,proj.window )
		proj.add=CreateButton("Add Project",280,4,100,24,proj.window)
		proj.remove=CreateButton("Remove Project",280,34,100,24,proj.window)
		proj.props=CreateButton("Properties",280,64,100,24,proj.window)
		proj.moveup=CreateButton("Move Up",280,94,100,24,proj.window)
		proj.movedown=CreateButton("Move Down",280,124,100,24,proj.window)
		DisableGadget proj.props 
		Return proj
	End Function
End Type

Type TFolderNode Extends TDataNode
'	Field	host:THost
	Field	path$
	Field	filter$
	Field	scanned
	Field	owner:TDataNode
	Global	commands:TMap=New TMap
	
	Method FindProject:TProjects()
		Local n:TDataNode
		n=Self
		While n
			If TProjects(n) Return TProjects(n)
			n=n.parent
		Wend		
	End Method
	
	Method OnMenu()
		Local project:TProjects
		project=findProject()
		PopupWindowMenu project.host.window,project.host.fileopmenu(),Self
	End Method

	Method Open(view=-1)
		If Not scanned
			ScanKids		'Path name,path
		EndIf
		Super.Open view
	End Method

	Method AddFileNode:TDataNode(file$)
		Local	n:TDataNode
		Local 	ext$
		Local	filetypes$
		
		n=AddNode(StripDir(file))
		n.SetAction(owner,"OPEN",file)
		ext=ExtractExt(file$).ToLower()
		n.sortname=n.name+ext 'ext+n.name
		Return n
	End Method

	Method AddFolderNode:TDataNode(path$)
		Local	n:TDataNode
		n=TFolderNode.CreateFolderNode(path,filter)
		n.sortname=" "+n.name
		Append n
		Return n
	End Method

	Method Scan(o:TDataNode)
		Local	flist:TList
		Local	dir,f$,p$,t	
		owner=o
		flist=New TList
		dir=ReadDir(path)
		If dir
			Repeat
				f$=NextFile(dir)
				If f$="" Exit
				If f[..1]="." Continue
				p$=path+"/"+f
				Select FileType(p$)
				Case FILETYPE_FILE
					If Not filter Or filter.find(ExtractExt(p).toLower())>=0				
						AddFileNode p$
					EndIf
				Case FILETYPE_DIR
					AddFolderNode p$
				End Select				
			Forever
			CloseDir dir
		EndIf
		SortKids
	End Method
	
	Method ScanKids()
		Local	f:TFolderNode		
		If scanned Return
		For f=EachIn kids
			f.Scan owner
		Next
		scanned=True
	End Method
	
	Method RefreshFolder()
		FreeKids
		Scan owner
		scanned=False
		ScanKids
		Refresh
	End Method
	
	Method Invoke(command$,argument:Object=Null)
		Local tool:TTool
		
		Select command
		Case "COMMAND"
			tool=TTool(commands.valueforkey(argument))
			If tool tool.Invoke String(argument),path
'			toolmap.
'			DebugStop
'			Select String(argument)
'				Case "Refresh"
'					RefreshFolder
'				Case "Checkout"
'					If SVNCheckout(path)
'						RefreshFolder
'					EndIf
'				Case "Update"
'					SVNUpdate path
'				Case "Commit"
'					RefreshFolder
'			End Select
		Default
			super.Invoke command,argument
		End Select
	End Method

	Function CreateFolderNode:TFolderNode(path$,filter$)
		Local	n:TFolderNode
		n=New TFolderNode
		n.path=path
		n.filter=filter
		Local p$
		p=StripDir(path)
		If p="" 
			p=StripDir(path[..path.length-1])
			DebugLog p
		EndIf
		n.setname p
		Return n
	End Function
End Type

Type TProjects Extends TDataNode
	Field	host:THost
	Field	addproj:TDataNode	
		
	Method RemoveProject(index)
		Local	node:TDataNode
		If index<0 Or index>=kids.Count() Return
		node=TDataNode(kids.ValueAtIndex(index))
		If node node.Free
		Refresh
	End Method			
	
	Method MoveProject(index,dir)
		Local node:TDataNode
		Local link:TLink
		If index<0 Or index>=kids.Count() Return index
		node=TDataNode(kids.ValueAtIndex(index))
		If node 
			addproj.Detach
			node.Hide
			link=kids.FindLink(node)
			If dir>0
				If link link=link._succ
				If link 
					kids.Remove node
					kids.InsertAfterLink node,link
					index:+1
				EndIf
			Else
				If link link=link._pred
				If link 
					kids.Remove node
					kids.InsertBeforeLink node,link			
					index:-1
				EndIf
			EndIf		
			Append addproj
			Refresh
		EndIf
		Return index
	End Method			
	
	Method AddProject()
		Local	path$,name$,stream:TStream
		path=RequestDir("Select the project folder")
		If Not path Return
		Add path,path
	End Method

	Method Add(name$,path$)
		Local folder:TFolderNode
		addproj.Detach
		folder=New TFolderNode.CreateFolderNode(path,host.FileTypes())
		folder.Scan Self
		Append folder
		Append addproj
		Refresh
	End Method

	Method Write(stream:TStream,indent$)
		Local	project:TFolderNode
		For project=EachIn kids
			stream.WriteLine indent+"proj_node="+project.name+"|"+project.path'node.argument.ToString()
		Next
	End Method
	
	Method Invoke(command$,argument:Object=Null)
		Select command
		Case "NEW"
			AddProject
		Case "REFRESH"
			DebugStop
'		Case "OPEN"
'			host.OpenSource String(argument)
'		Case "COMMAND"
'			Select String(argument)
'				Case "Refresh"
'			End Select
		End Select
	End Method
			
	Function CreateProjects:TProjects(host:THost)
		Local	p:TProjects
		p=New TProjects
		p.SetName("Projects")
		p.host=host
		p.addproj=p.AddNode("Add Project")
		p.addproj.SetAction p,"NEW"
		host.AddFileOperation "Refresh",p		
		Return p
	End Function
End Type

EndRem
